﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenSunTracker.RestClient;
using OpenSunTracker.RestClient.Extensions;

namespace OSTWin
{
    /// <summary>
    /// Interaction logic for ucMotor.xaml
    /// </summary>
    public partial class ucMotor : UserControl
    {
        public ucMotor()
        {
            InitializeComponent();
        }

        public OpenSunTrackerClient Client { get { return DataContext as OpenSunTrackerClient; } }

        private void btnInit_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/init";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-init (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-init (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnCali_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/calibrate";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-cali (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-cali (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnDrive2_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = string.Format(Client.Url + "/motor/drive2?position={0}", txtDrive2.Text);
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-drive2 (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-drive2 (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnDrive2Center_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/drive2center";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-drive2center (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-drive2center (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/stop";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-stop (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-stop (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnMoveUp_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/moveup";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-moveup (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-moveup (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnMoveDown_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/motor/movedown";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("motor-movedown (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on motor-movedown (" + actionUrl + "): " + ex.Message);
            }
        }
    }
}
